package com.huonan.jb.tools.fileuse.compare;

import cn.hutool.cache.CacheUtil;
import cn.hutool.cache.impl.FIFOCache;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.date.TimeInterval;
import com.huonan.utils.ExecutorUtil;
import lombok.extern.slf4j.Slf4j;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;

import static com.huonan.jb.tools.fileuse.compare.StaticsHandler.printStaticInfo;

/**
 * @Description 原始文件问题定位: 统计原始文件内容数据量
 * @Author lxp
 * @Date 2021/11/23 上午10:13
 * @Version V1.0
 **/
@Slf4j
public class AppFileStatic {

    public static void main(String[] args) {
        for (String arg : args) {
            log.info("arg: {}", arg);
        }
        /*输入文件路径*/
        String sourcePath = args[0];
        /*输出文件路径*/
        String outDir = args[1];
        int depth = Integer.parseInt(args[2]);
        handler(sourcePath, outDir, depth);
    }

    private static void handler(String sourcePath, String outDir, int depth) {
        ExecutorService threadPool = ExecutorUtil.getExecutorService(3);
        List<File> allFiles = FileHandler.traverseAllFiles(sourcePath, depth);
        log.info("总文件数量:{}", allFiles.size());
        List<CompletableFuture<String>> list = new ArrayList<>();
        TimeInterval timer = DateUtil.timer();
        allFiles.forEach(file -> {
            log.info("正在处理文件:{}", file);
            CompletableFuture<String> future = StaticsHandler.staticByFile(threadPool, file);
            list.add(future);
        });
        ExecutorUtil.closePool(threadPool);
        blockThread(list);
        printStaticInfo();
        long takeTime = timer.intervalSecond();
        log.info("take time:{} s", takeTime);
    }

    private static void blockThread(List<CompletableFuture<String>> list) {
        list.forEach(f -> {
            try {
                f.get();
            } catch (InterruptedException | ExecutionException e) {
                throw new RuntimeException(e);
            }
        });
    }
}
